package com.app.micaihu.view.army.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.AttentionAndFansBean
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.view.army.adapter.UserFollowListAdapter
import com.app.micaihu.view.army.contract.UserFollowContract
import com.app.micaihu.view.army.presenter.UserFollowListPresenter
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.view_common_rv.*

/**
 * 邀请 关注列表
 */
class UserFollowListFragment : BaseMvpFragment<UserFollowContract.View, UserFollowListPresenter>(), UserFollowContract.View {

    override fun getLayoutId() = R.layout.view_common_rv
    override fun getPresenter() = UserFollowListPresenter(1)
    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
    }

    private var gid = ""
    private var index = 0
    private val userFollowListAdapter: UserFollowListAdapter by lazy {
        val userFollowListAdapter = UserFollowListAdapter()
        rvList.layoutManager = LinearLayoutManager(context)
        rvList.adapter = userFollowListAdapter

        userFollowListAdapter
    }

    override fun setData() {
        refreshLayout.setEnableLoadMore(true)
        refreshLayout.setEnableAutoLoadMore(true)
    }

    override fun lazyLoad() {
        userFollowListAdapter.setEmptyView(loadingView)
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.refresh()
        }
        userFollowListAdapter.addChildClickViewIds(R.id.tvInvite)
        userFollowListAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.tvInvite -> {
                    index = position
                    val item = userFollowListAdapter.getItem(position)
                    mPresenter?.armyInvite(gid, item.uid)
                }
            }
        }
        userFollowListAdapter.setOnItemClickListener { adapter, view, position ->
        }
        refreshLayout.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
    }

    override fun handleFollowList(result: BaseBean<List<AttentionAndFansBean>?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        if (result == null) {
            if (userFollowListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    refreshLayout.finishLoadMore(false)
                }
            } else {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    userFollowListAdapter.setEmptyView(errorView)
                } else {
                    userFollowListAdapter.setEmptyView(getEmptyView())
                }
            }
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        userFollowListAdapter.setList(it)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        userFollowListAdapter.addData(it)
                    }
                    if (it.isNullOrEmpty()) {
                        userFollowListAdapter.setEmptyView(getEmptyView())
                        refreshLayout.finishLoadMoreWithNoMoreData()
                    } else {
                        refreshLayout.finishLoadMore(Config.delayLoadMore)
                    }
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }

    }

    override fun handleArmyInvite(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                val item = userFollowListAdapter.getItem(index)
                item.isArmyStatus = "1"
                userFollowListAdapter.notifyItemChanged(index)
            }
            ToastUtils.showShort(result.msg)
        }
    }


}